<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      label-width="68px"
    >
      <el-form-item label="机构名称">
        <el-select
          v-model="queryParams.institutionId"
          placeholder="请选择机构名称"
          filterable
          @change="getStoreList"
        >
          <el-option
            v-for="item in institutionList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="门店名称" prop="storeId">
        <el-select
          v-model="queryParams.storeId"
          placeholder="请选择门店名称"
          filterable
          @change="handleQuery"
        >
          <el-option
            v-for="item in storeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['mc:inventory:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >
          新增取药订单
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="药品名称" align="center" prop="medicineName" />
      <el-table-column label="条码" align="center" prop="barCode" />
      <el-table-column label="生产厂家" align="center" prop="manufacturer" />
      <el-table-column label="处方分类" align="center">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.mc_prescription_type"
            :value="scope.row.prescriptionType"
          />
        </template>
      </el-table-column>
      <el-table-column label="药品规格" align="center" prop="medicineSpecifications" />
      <el-table-column label="库存" align="center" prop="inventory" />
      <el-table-column label="单位" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mc_medicine_unit" :value="scope.row.unit" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleView(scope.row)">
            查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <Edit-dialog v-model="open" :mc_medicine_variety_type="dict.type.mc_medicine_variety_type" />
    <Medicine-dialog
      v-model="detailShow"
      :info="detailInfo"
      :mc_prescription_type="dict.type.mc_prescription_type"
      :mc_medicine_variety_type="dict.type.mc_medicine_variety_type"
      :mc_medicine_unit="dict.type.mc_medicine_unit"
      :mc_dosage_form="dict.type.mc_dosage_form"
    />
  </div>
</template>

<script>
import { listInstitution } from "@/api/mc/institution";
import { listStore } from "@/api/mc/store";
import { inventoryMedicineList } from "@/api/mc/inventory";

import MedicineDialog from "./components/MedicineDialog.vue";
import EditDialog from "./components/EditDialog.vue";

export default {
  name: "Inventory",
  components: { MedicineDialog, EditDialog },
  dicts: [
    "mc_prescription_type",
    "mc_medicine_variety_type",
    "mc_medicine_unit",
    "mc_dosage_form",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 货道药品库存管理表格数据
      dataList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        institutionId: null,
        storeId: null,
      },
      institutionList: [], // 机构列表
      storeList: [], // 门店列表
      detailShow: false, // 药品详情弹窗
      detailInfo: {}, // 药品详情
    };
  },
  watch: {
    open(val) {
      if (!val) {
        this.handleQuery();
      }
    },
  },
  created() {
    this.getInstitutionList();
  },
  methods: {
    /** 查询货道药品库存管理列表 */
    getList() {
      this.loading = true;
      inventoryMedicineList(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 获取机构列表
    getInstitutionList() {
      listInstitution({ pageNum: 1, pageSize: 1000 }).then((res) => {
        this.institutionList = res.rows;
        if (res.rows.length > 0) {
          this.queryParams.institutionId = res.rows[0].id;
          this.getStoreList();
        }
      });
    },
    // 获取门店列表
    getStoreList() {
      listStore({
        pageNum: 1,
        pageSize: 1000,
        institutionId: this.queryParams.institutionId,
      }).then((res) => {
        this.storeList = res.rows || [];
        if (res.rows.length > 0) {
          this.queryParams.storeId = res.rows[0].id;
        } else {
          this.queryParams.storeId = "";
        }
        this.handleQuery();
      });
    },
    // 查看详情
    handleView(row) {
      this.detailInfo = row;
      this.detailShow = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
    },
  },
};
</script>
